/* eslint-disable */
import { TreeArrayType, ITree } from './tree'

const data = [
  { id: 1, name: '用户管理', pid: 0 },
  { id: 2, name: '菜单申请', pid: 1 },
  { id: 3, name: '信息申请', pid: 1 },
  { id: 4, name: '模块记录', pid: 2 },
  { id: 5, name: '系统设置', pid: 0 },
  { id: 6, name: '权限管理', pid: 5 },
  { id: 7, name: '用户角色', pid: 6 },
  { id: 8, name: '菜单设置', pid: 6 },
]

class Tree<T> {
  root: TreeArrayType[]
  data: TreeArrayType[]
  str: string
  constructor(options: T) {
    this.root = []
    this.str = ''
    this.data = (options as any).data

    this.init(this.data)
  }

  getList() {
    return this.root
  }

  init(data: TreeArrayType[]) {
    const parents = data.filter((item) => item.pid === 0)
    const children = data.filter((v) => v.pid !== 0)
    this.dataToTree(parents, children)
    this.root = parents
    // return parents
  }

  // 递归
  dataToTree(parents: TreeArrayType[], children: TreeArrayType[]) {
    parents.forEach((element) => {
      children.forEach((item, i) => {
        if (element.id === item.pid) {
          let _c = JSON.parse(JSON.stringify(children))
          _c.splice(i, 1)
          this.dataToTree([item], _c)
          if (element.children) {
            element.children.push(item)
          } else {
            element.children = [item]
          }
        }
      })
    })
  }

  render(data: TreeArrayType[]) {
    // 使用vue 做一个递归组件
    // 拿到this.root遍历循环成字符串，一次性抛出
  }
}

let it = new Tree<ITree>({ root: [], data: data })

let list = it.getList()

export { list }
/* eslint-disable */
